/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.models.impl;

import com.ntb.models.*;
import com.ntb.common.DatabaseConnection;
import com.ntb.entities.StampDutyAndRegistration;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author AnhTuan
 */
public class StampDutyAndRegistrationDAOImpl extends DatabaseConnection implements StampDutyAndRegistrationDAO {

    public Iterator<StampDutyAndRegistration> getList() {
        try {
            ArrayList list = new ArrayList();
            openConnection();
            ResultSet rs = getResultset("Select * From StampDutyAndRegistration");
            while (rs.next()) {
                StampDutyAndRegistration row = new StampDutyAndRegistration();
                row.setId(rs.getInt("ID"));
                row.setSdCharge(rs.getInt("SDCharge"));
                row.setStartDate(rs.getTimestamp("StartDate"));

                list.add(row);
            }
            closeConnection();
            return list.iterator();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public Iterator<StampDutyAndRegistration> getList(int page) {
        try {
            ArrayList list = new ArrayList();
            openConnection();
            CallableStatement cst = conn.prepareCall("{call spu_pageStampDutyAndRegistration(?, ?)}");
            cst.setInt(1, page);
            cst.setInt(2, itemsPerPage);
            ResultSet rs = cst.executeQuery();
            while (rs.next()) {
                StampDutyAndRegistration row = new StampDutyAndRegistration();
                row.setId(rs.getInt("ID"));
                row.setSdCharge(rs.getInt("SDCharge"));
                row.setStartDate(rs.getTimestamp("StartDate"));

                list.add(row);
            }
            closeConnection();
            if (list.size() > 0) {
                return list.iterator();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public int getPageCount() {
        try {
            openConnection();
            CallableStatement cst = conn.prepareCall("{call spu_countStampDutyAndRegistration(?)}");
            cst.setInt(1, itemsPerPage);
            ResultSet rs = cst.executeQuery();
            int count = 0;
            if (rs.next()) {
                count = rs.getInt("Count");
            }
            closeConnection();

            return count;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return 0;
    }

    public StampDutyAndRegistration getRow(int id) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Select * From StampDutyAndRegistration Where ID=?");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            StampDutyAndRegistration row = new StampDutyAndRegistration();
            if (rs.next()) {
                row.setId(rs.getInt("ID"));
                row.setSdCharge(rs.getInt("SDCharge"));
                row.setStartDate(rs.getTimestamp("StartDate"));
            }
            closeConnection();
            return row;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public StampDutyAndRegistration getLast() {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Select TOP 1 * From StampDutyAndRegistration ORDER BY ID DESC");
            ResultSet rs = pst.executeQuery();
            StampDutyAndRegistration row = new StampDutyAndRegistration();
            if (rs.next()) {
                row.setId(rs.getInt("ID"));
                row.setSdCharge(rs.getInt("SDCharge"));
                row.setStartDate(rs.getTimestamp("StartDate"));
                closeConnection();
                return row;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public boolean checkDuplicate(int sdCharge, Timestamp startDate) {
        try {
            closeConnection();
            PreparedStatement pst = conn.prepareStatement("Select * From StampDutyAndRegistration Where SDCharge=? AND StartDate=?");
            pst.setInt(1, sdCharge);
            pst.setTimestamp(2, startDate);
            ResultSet rs = pst.executeQuery();
            return rs.next();
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public int add(StampDutyAndRegistration object) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Insert Into StampDutyAndRegistration(SDCharge, StartDate) Values(?,?)");
            pst.setInt(1, object.getSdCharge());
            pst.setTimestamp(2, new Timestamp(new java.util.Date().getTime()));
            int result = pst.executeUpdate();
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public int update(StampDutyAndRegistration object) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Update StampDutyAndRegistration Set SDCharge=? Where ID=?");
            pst.setInt(1, object.getSdCharge());
            pst.setInt(2, object.getId());
            int result = pst.executeUpdate();
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public int delete(int id) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Delete StampDutyAndRegistration Where ID=?");
            pst.setInt(1, id);
            int result = pst.executeUpdate();
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public int deleteIn(String arrayIds) {
        try {
            openConnection();
            String sql = "Delete StampDutyAndRegistration Where ID in (" + arrayIds + ")";
            Statement st = conn.createStatement();
            int result = st.executeUpdate(sql);
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }
}
